package com.zhuangjie.order.controller;

import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.order.service.ShopSSEService;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.shop.helper.ShopModuleTokenDataProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

/**
 * 店铺SSE 控制器
 */
@RequestMapping("/sse/shop")
@RestController
@PublicAPI(modules = {ModuleSecurityNames.SHOP})
public class ShopSSEController {
    @Autowired
    private ShopSSEService shopSSEService;
    @GetMapping("/on")
    public SseEmitter on() throws IOException {
        Long shopId = ShopModuleTokenDataProvider.getShopId();
        return shopSSEService.listen(shopId);
    }

    @GetMapping("/close")
    public void close() {
        Long shopId = ShopModuleTokenDataProvider.currentUserId();
        shopSSEService.remove(shopId);
    }
}
